package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import edu.chinasoft.pojo.City;
import edu.chinasoft.pojo.Province;
import edu.chinasoft.service.ProvinceService;
import edu.chinasoft.service.ProvinceServiceImp;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myFindCityByProcinceServlet02", value = "/servlet/findcitybyprocinceservlet02")
public class FindCityByProcinceServlet02 extends HttpServlet {
	private static Logger logger = Logger.getLogger(FindCityByProcinceServlet02.class);
	private ProvinceService provinceService=new ProvinceServiceImp();
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" FindCityByProcinceServlet02 >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		logger.debug(" FindCityByProcinceServlet02 >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" FindCityByProcinceServlet02 >doPost start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//后台禁止使用缓存
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		response.setDateHeader("expires", 0);
		PrintWriter out = response.getWriter();
		//获取后台传送过来的数据
		String provinceidString = request.getParameter("provinceid");
		int provinceidInt = Integer.parseInt(provinceidString);
		//创建一个省对象，调用服务层方法把该省所对应的市区获取到
		Province province = new Province();
		province.setId(provinceidInt);
		Province resultProvince = provinceService.findCityByProvince(province);
		//id:name|id:name|id:name
		String resultString="";
		for(City city:resultProvince.getCitys()) {
			resultString+=city.getId()+":"+city.getCityName()+"|";
		}
		//需要把最后一个|去掉
		resultString=resultString.substring(0,resultString.length()-1);
		logger.debug("返回的内容为："+resultString+"。");
		//把内容返回给浏览器
		out.write(resultString);
		logger.debug(" FindCityByProcinceServlet02 >doPost end.......");
	}

}